package com.example.hanfu.tiku;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class DBTikuManager {
    private DatiDBHelper dbHelper;
    private String TBNAME;

    private static final String TAG = "DBTikuManager";
    public DBTikuManager(Context context) {
        dbHelper = new DatiDBHelper(context);
        TBNAME = DatiDBHelper.TB_NAME;
    }

    public void deleteAll(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(TBNAME,null,null);
        db.close();
    }

    public void delete(int id){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(TBNAME, "id=?", new String[]{String.valueOf(id)});
        db.close();
    }
    public void add(Tiku item){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("topic", item.getTopic());
        values.put("option_a", item.getOption_a());
        values.put("option_b", item.getOption_b());
        values.put("option_c", item.getOption_c());
        values.put("option_d", item.getOption_d());
        values.put("answer",item.getAnswer());
        values.put("corAnswer",item.getCorAnswer());
        db.insert(TBNAME, null, values);
        db.close();
    }
    public Tiku findById(int id){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, "ID=?", new String[]{String.valueOf(id)}, null, null, null);
        Tiku item = null;
        if(cursor!=null && cursor.moveToFirst()){
            item = new Tiku();
            item.setId(cursor.getInt(cursor.getColumnIndex("id")));
            item.setTopic(cursor.getString(cursor.getColumnIndex("topic")));
            item.setOption_a(cursor.getString(cursor.getColumnIndex("option_a")));
            item.setOption_b(cursor.getString(cursor.getColumnIndex("option_b")));
            item.setOption_c(cursor.getString(cursor.getColumnIndex("option_c")));
            item.setOption_d(cursor.getString(cursor.getColumnIndex("option_d")));
            item.setAnswer(cursor.getInt(cursor.getColumnIndex("answer")));
            item.setCorAnswer(cursor.getString(cursor.getColumnIndex("corAnswer")));
            item.setSelectedAnswer(-1);
            cursor.close();
        }
        db.close();
        return item;
    }


    public List<Tiku> listAll(){
        List<Tiku> list = null;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, null, null, null, null, null);
        if(cursor!=null){
            list = new ArrayList<Tiku>();
            while(cursor.moveToNext()){
                Tiku item = new Tiku();
                item.setId(cursor.getInt(cursor.getColumnIndex("id")));
                item.setTopic(cursor.getString(cursor.getColumnIndex("topic")));
                Log.i(TAG,cursor.getString(cursor.getColumnIndex("topic")));
                item.setOption_a(cursor.getString(cursor.getColumnIndex("option_a")));
                item.setOption_b(cursor.getString(cursor.getColumnIndex("option_b")));
                item.setOption_c(cursor.getString(cursor.getColumnIndex("option_c")));
                item.setOption_d(cursor.getString(cursor.getColumnIndex("option_d")));
                item.setAnswer(cursor.getInt(cursor.getColumnIndex("answer")));
                item.setCorAnswer(cursor.getString(cursor.getColumnIndex("corAnswer")));
                item.setSelectedAnswer(-1);
                list.add(item);
            }
            cursor.close();
        }
        db.close();
        return list;

    }
}
